<script setup lang="ts">
import PageHeader from "@/views/Common/PageHeader.vue";
import {ref, onMounted, onBeforeMount} from "vue";
import http from "@/utils/http.js";
import {useUserStore} from '@/stores/user'
import {ElMessage} from "element-plus";
import {askExpertAPI} from '@/apis/AI'
import {RegionFullText} from "v-region";
import {getExpertInfoAPI,getQuestionsByUidAPI} from '@/apis/expert'
import {useRouter} from "vue-router";

const router = useRouter()
const expertInfo = ref({})
const UserQuestions = ref([])

onMounted(()=>{
  getExpertInfo()
  getQuestion()
})

async function getExpertInfo() {
  const res=await getExpertInfoAPI(router.currentRoute.value.params.uid)
  console.log(res)
  if (res.code===200){
    expertInfo.value=res.data
    const message={
      role:'expert',
      message:`你好，我是${expertInfo.value.name}，欢迎向我提问~`,
      time:''
    }
    messageList.value.push(message)
  }
}

async function getQuestion(){
  const res=await getQuestionsByUidAPI(router.currentRoute.value.params.uid)
  console.log(res)
  if(res.code===200){
    UserQuestions.value = res.data
  }
}


const messageList=ref<MESSAGE>([])
const userAvatar=useUserStore().getAvatar
const inputRef=ref()
const scrollRef=ref()//聊天窗口滚动条
const canSend=ref(true)//是否能发送，当AI正在回答时，不能发送消息

interface MESSAGE{
  role:string
  message:string,
  time:string
}

function sendMsg() {
  if(canSend.value===false){
    ElMessage({
      type:'warning',
      message:'请稍等'
    })
    return
  }
  if(inputRef.value===''){
    ElMessage({
      type:'warning',
      message:'请输入内容'
    })
    return
  }

  const msg:MESSAGE={
    role:'user',
    message:inputRef.value,
    time:new Date().getTime()
  }
  messageList.value.push(msg)
  //调用AI
  askExpert(inputRef.value)
  inputRef.value=''
  scrollRef.value.scrollTop = scrollRef.value.scrollHeight
}

async function askExpert(text) {
  canSend.value=false//开始回答
  const completion= await askExpertAPI(text)
    const answer:MESSAGE={
      role:'expert',
      message:'',
      time:new Date().getTime()
    }
    messageList.value.push(answer)
  //把响应流的中的chunk不断拼接到message中，实现流式输出的效果
    for await (const chunk of completion) {
      if(chunk.choices[0].finish_reason!=='stop'){
        messageList.value[messageList.value.length-1].message+=chunk.choices[0].delta.content
      }
      scrollRef.value.scrollTop = scrollRef.value.scrollHeight//滚动条自动滚动到底部
    }
    canSend.value=true//回答完成
}


</script>

<template>
  <div class="myInfoPage">

    <div>
      <PageHeader></PageHeader>
    </div>

    <div class="header">
      <img style="height: 60px;width: auto;margin-left: 40px;margin-top: 20px" src="@/assets/SAS-logo.png" alt="">
      <el-button 
        type="primary" 
        @click="router.push('/expertHomePage')" 
        style="margin-left: auto; margin-right: 40px; margin-top: 20px;"
      >
        返回主页
      </el-button>
    </div>

    <div class="page-content">

      <div class="left">
        <div class="expert-info">
          <div class="info-top">
            <div class="top-l">
              <div class="top-header">
                <el-avatar :src="expertInfo.expert_avatar" style="height: 99px;width: 99px"/>
              </div>
            </div>
            <div class="top-r">
              <span class="info-line" style="font-size: 24px;font-weight: bold" >{{expertInfo.name}}·{{expertInfo.position}}</span>
              <RegionFullText class="info-line" separator="-" v-model="expertInfo"/>&nbsp;
              <span class="info-line">{{expertInfo.belong}}</span>
              <span class="info-line">{{expertInfo.descri}}</span>
            </div>
          </div>

          <div class="info-foot">
            <div class="foot-l">
              <span class="info-line" style="font-size: 25px">{{expertInfo.guidances}}</span>
              <span style="font-size: 25px">咨询量</span>
            </div>
            <div class="foot-r">
              <span class="info-line" style="font-size: 25px">{{expertInfo.replies}}</span>
              <span style="font-size: 25px">回复量</span>
            </div>
          </div>
        </div>

        <div class="expert-ques">
          <div class="ques-header">
            <div class="title"><span style="font-size: 25px">专家问答</span></div>
            <div class="more"><el-link :underline="false" style="font-size: 18px" @click="router.push('/expert/questionPage')">
              更多
              <img src="@/assets/right-arrow.png" style="height: 25px;width: 25px" alt="右箭头"/>
            </el-link>
            </div>
          </div>

          <div class="ques-content" v-for="(item, index) in UserQuestions" :key="index"
               @click="router.push(`/question/${item.qid}`)">
            <div class="question" style="margin-left: 25px;font-weight: bold;margin-top: 25px">
              <el-badge value="问" color="#14c131"></el-badge>
              <span>{{item.user_ques}}</span>
            </div>
            <div class="answer" style="margin-left: 25px;font-weight: bold;margin-top: 15px">
              <el-badge value="答" color="#ff862c"></el-badge>
              <span>{{item.expert_ans}}</span>
            </div>
            <div class="footer" style="margin-top: 5px">
              <span style="float: left;margin-left: 50px">全部1个回答</span>
              <span style="margin-left: 50px;color: darkred;font-weight: bold">采纳数:{{item.adoptNumbers}}</span>
              <span style="float: right;margin-right: 30px">{{item.expert_date}}</span>
            </div>
            <el-divider style="margin-bottom: 10px"/>
          </div>
        </div>

      </div>

      <div class="right" >

        <div class="chatHeader" >
          <span style="line-height: 60px">AI专家·在线问答</span>
        </div>

        <div class="chatArea" ref="scrollRef">

          <div v-for="msg in messageList">
          <div class="expert" v-if="msg.role==='expert'">
            <img class="avatar" :src="expertInfo.expert_avatar">
            <div class="message">
              <span>{{msg.message}}</span>
            </div>
          </div>
          <div class="user" v-else-if="msg.role==='user'">
            <div class="message" style="background-color: #3ae240;margin-right: 10px">
              <span>{{msg.message}}</span>
            </div>
            <img class="avatar" :src="userAvatar">
          </div>
        </div>
        </div>
        <div class="input-frame">
          <el-input style="height: 50px" v-model="inputRef" @keyup.enter.native="sendMsg">
            <template #suffix>
              <el-button @click="sendMsg" >发送</el-button>
            </template>
          </el-input>
        </div>
      </div>


    </div>

  </div>

</template>

<style scoped lang="less">

.myInfoPage {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.ques-content {
  padding: 20px;
  margin-bottom: 15px;
  background: #fafafa;
  border-radius: 12px;
  border: 1px solid #f0f0f0;
  transition: all 0.3s ease;
  cursor: pointer;
  
  &:hover {
    background: white;
    border-color: #52c41a;
    box-shadow: 0 4px 15px rgba(82, 196, 26, 0.1);
    transform: translateY(-2px);
    
    .question {
      color: #52c41a;
    }
  }
  
  .question {
    margin-left: 25px;
    font-weight: 600;
    margin-top: 0;
    margin-bottom: 15px;
    font-size: 15px;
    color: #2c3e50;
    transition: color 0.3s ease;
    
    .el-badge {
      margin-right: 10px;
      
      .el-badge__content {
        font-size: 11px;
        padding: 0 6px;
        height: 18px;
        line-height: 18px;
      }
    }
  }
  
  .answer {
    margin-left: 25px;
    font-weight: 500;
    margin-top: 15px;
    font-size: 14px;
    color: #6c757d;
    line-height: 1.5;
    
    .el-badge {
      margin-right: 10px;
      
      .el-badge__content {
        font-size: 11px;
        padding: 0 6px;
        height: 18px;
        line-height: 18px;
      }
    }
  }
  
  .footer {
    margin-top: 15px;
    padding-top: 12px;
    border-top: 1px solid #f0f0f0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    
    span {
      &:first-child {
        color: #6c757d;
      }
      
      &:nth-child(2) {
        color: #ff6b35;
        font-weight: 600;
      }
      
      &:last-child {
        color: #6c757d;
      }
    }
  }
  
  .el-divider {
    margin: 0;
    border-color: #f0f0f0;
  }
}

//页面头
.header {
  width: 100%;
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

//专家标题
.title{
  float: left;
}

//更多详情
.more{
  float: right;
}

//专家表头
.ques-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 2px solid #f0f0f0;
  
  .title {
    span {
      font-size: 22px;
      font-weight: 700;
      color: #1a1a1a;
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        bottom: -5px;
        left: 0;
        width: 40px;
        height: 3px;
        background: linear-gradient(90deg, #52c41a, #73d13d);
        border-radius: 2px;
      }
    }
  }
  
  .more {
    .el-link {
      display: flex;
      align-items: center;
      gap: 8px;
      color: #52c41a;
      font-weight: 600;
      transition: all 0.3s ease;
      
      &:hover {
        color: #73d13d;
        transform: translateX(3px);
      }
      
      img {
        transition: transform 0.3s ease;
      }
      
      &:hover img {
        transform: translateX(3px);
      }
    }
  }
}

//页面中心内容
.page-content {
  max-width: 1400px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr 400px;
  gap: 30px;
  padding: 0 20px;
}

.right {
  display: flex;
  flex-direction: column;
  background: white;
  border-radius: 20px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  height: fit-content;
  max-height: 85vh;
  position: sticky;
  top: 20px;
}

.chatArea {
  width: 100%;
  height: 600px;
  padding: 20px 15px;
  display: flex;
  flex-direction: column;
  gap: 15px;
  overflow-y: auto;
  background: #fafafa;
  
  &::-webkit-scrollbar {
    width: 6px;
  }
  
  &::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 3px;
  }
  
  &::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 3px;
    
    &:hover {
      background: #a8a8a8;
    }
  }
}
.chatHeader {
  width: 100%;
  height: 60px;
  background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%);
  color: white;
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  line-height: 60px;
  box-shadow: 0 4px 15px rgba(82, 196, 26, 0.3);
  position: relative;
  
  &::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
    animation: shimmer 2s infinite;
  }
}
.expert {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  animation: fadeInUp 0.3s ease;
}

.user {
  display: flex;
  justify-content: flex-end;
  align-items: flex-start;
  gap: 12px;
  animation: fadeInUp 0.3s ease;
}

.avatar {
  width: 45px;
  height: 45px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  flex-shrink: 0;
}

.message {
  padding: 12px 16px;
  max-width: 280px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  border-radius: 18px;
  position: relative;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  transition: all 0.2s ease;
  
  &:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
  }
  
  .expert & {
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    color: #2c3e50;
    border-bottom-left-radius: 4px;
    
    &::before {
      content: '';
      position: absolute;
      left: -8px;
      top: 18px;
      width: 0;
      height: 0;
      border-top: 8px solid transparent;
      border-bottom: 8px solid transparent;
      border-right: 8px solid #e9ecef;
    }
  }
  
  .user & {
    background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%);
    color: white;
    border-bottom-right-radius: 4px;
    
    &::before {
      content: '';
      position: absolute;
      right: -8px;
      top: 18px;
      width: 0;
      height: 0;
      border-top: 8px solid transparent;
      border-bottom: 8px solid transparent;
      border-left: 8px solid #73d13d;
    }
  }
}

.router-view {
  min-width: 1000px;
  height: 500px;
  //background-color: #409EFF;
}

//往后

.expert-info {
  background: white;
  border-radius: 20px;
  padding: 30px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
  }
}

.expert-ques {
  background: white;
  border-radius: 20px;
  padding: 25px;
  margin-top: 30px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
  }
}

//.expert-ques {
//
//  width: 300px;
//  background: #fff;
//
//  border-radius: 8px;
//}

.info-top {
  display: flex;
  align-items: flex-start;
  gap: 25px;
  margin-bottom: 30px;
}

.top-l {
  position: relative;
  
  .el-avatar {
    width: 100px !important;
    height: 100px !important;
    border: 4px solid #fff;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    transition: all 0.3s ease;
    
    &:hover {
      transform: scale(1.05);
      box-shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
    }
  }
}

.top-r {
  flex: 1;
  
  .info-line {
    display: block;
    margin-bottom: 12px;
    font-size: 16px;
    color: #2c3e50;
    line-height: 1.5;
    
    &:first-child {
      font-size: 24px;
      font-weight: 700;
      color: #1a1a1a;
      margin-bottom: 16px;
    }
    
    &:not(:first-child) {
      position: relative;
      padding-left: 20px;
      
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 8px;
        width: 6px;
        height: 6px;
        background: #52c41a;
        border-radius: 50%;
      }
    }
  }
}

.info-foot {
  display: flex;
  justify-content: space-around;
  padding: 25px 0;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 15px;
  margin-top: 20px;
}

.foot-l, .foot-r {
  text-align: center;
  flex: 1;
  
  .info-line {
    display: block;
    font-size: 32px;
    font-weight: 700;
    color: #52c41a;
    margin-bottom: 8px;
    text-shadow: 0 2px 4px rgba(82, 196, 26, 0.2);
  }
  
  span:last-child {
    font-size: 14px;
    color: #6c757d;
    font-weight: 500;
  }
}

.messages-container {
  flex: 1;
  overflow-y: auto;
}

// 动画效果
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes shimmer {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

// 响应式设计
@media (max-width: 1200px) {
  .page-content {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  .right {
    max-height: 600px;
    position: relative;
    top: 0;
  }
}

@media (max-width: 768px) {
  .myInfoPage {
    padding: 10px;
  }
  
  .info-top {
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: 15px;
  }
  
  .top-r .info-line {
    padding-left: 0;
    
    &::before {
      display: none;
    }
  }
  
  .info-foot {
    flex-direction: column;
    gap: 15px;
  }
}

.input-frame {
  padding: 20px;
  background: white;
  border-top: 1px solid #f0f0f0;
  
  .el-input {
    .el-input__wrapper {
      border-radius: 25px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
      border: 2px solid transparent;
      transition: all 0.3s ease;
      
      &:hover {
        border-color: #52c41a;
        box-shadow: 0 4px 12px rgba(82, 196, 26, 0.15);
      }
      
      &.is-focus {
        border-color: #52c41a;
        box-shadow: 0 4px 12px rgba(82, 196, 26, 0.25);
      }
    }
    
    .el-button {
      border-radius: 20px;
      background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%);
      border: none;
      color: white;
      font-weight: 600;
      padding: 8px 20px;
      transition: all 0.3s ease;
      
      &:hover {
        background: linear-gradient(135deg, #73d13d 0%, #52c41a 100%);
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(82, 196, 26, 0.3);
      }
    }
  }
}

</style>